package com.leyou.search.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpuDTO;
import com.leyou.search.pojo.Goods;
import com.leyou.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author xiuer
 * @description 箭头商品微服务消息
 * @create 2019/7/5 20:09
 */
@Component
@Slf4j
public class ListenerItemMQ {

    @Autowired
    private SearchService searchService;

    //接收MQ 商品上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.Queue.SEARCH_ITEM_UP, durable = "true"),
            exchange = @Exchange(
                    value = MQConstants.Exchange.ITEM_EXCHANGE_NAME,
                    type = ExchangeTypes.TOPIC
            ),
            key = MQConstants.RoutingKey.ITEM_UP_KEY))
    private void listenerItemMQup(Long id) {
        if (id != null) {
            searchService.createIndex(id);
        }
    }

    //接收MQ 商品下架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConstants.Queue.SEARCH_ITEM_DOWN, durable = "true"),
            exchange = @Exchange(
                    value = MQConstants.Exchange.ITEM_EXCHANGE_NAME,
                    type = ExchangeTypes.TOPIC
            ),
            key = MQConstants.RoutingKey.ITEM_DOWN_KEY))
    private void listenerItemMQdown(Long id) {
        if (id != null) {
            //删除指定的索引库数据
            searchService.deleteIndex(id);
        }
    }

}
